﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class System_Role : ATPages
{
    ZSql info = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = "";

        if (!IsPostBack)
        {
            Bind();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
        }
    }


    private void Bind()
    {
        info.Open("select a.roleid,a.role,(select count(0) from userinfo where roleid=a.roleid) as ProNum from role a order by a.roleid desc");
        gvData.DataSource = info.m_table;
        gvData.DataBind();
        info.Close();
    }


    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string roleid = (sender as LinkButton).CommandArgument;

        Response.Redirect("AddRole.aspx?rid=" + roleid+"&moduid="+Request["moduid"]);
    }



    protected void GvDataType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //f.flag(GvDataType, "SV_Type", "TypeId", 5);
        //f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 4, GvDataType, btndel, btnallflag, btnAdd);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string ProNum = DataBinder.Eval(e.Row.DataItem, "ProNum").ToString();
            if (Convert.ToInt32(ProNum) > 0)
            {
                (e.Row.Cells[4].FindControl("chkDelete") as CheckBox).Enabled = false;
            }
        }

    }

    protected void lnkbtnSet_Click(object sender, EventArgs e)
    {
        string roleid = (sender as LinkButton).CommandArgument;

        Response.Redirect("PurviewSet.aspx?id=" + roleid + "&t=1&moduid=" + Request["moduid"]);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddRole.aspx?moduid=" + Request["moduid"]);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

        Response.Redirect("AddRole.aspx?moduid=" + Request["moduid"]+"&rid=");
    }
    protected void Button3_Click(object sender, EventArgs e)
    {

        Response.Redirect("AddRole.aspx?moduid="+Request["moduid"]);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sql = "select a.roleid,a.role,(select count(0) from userinfo where roleid=a.roleid) as ProNum from role a where 1=1 ";
        if (!string.IsNullOrEmpty(TextBox1.Text))
        {
            sql += " and a.role='"+Common.FormatParameter(TextBox1.Text)+"'";
        }
        sql += " order by a.roleid desc";
        info.Open(sql);
        gvData.DataSource = info.m_table;
        gvData.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (gvData.Rows.Count > 0)
        {
            for (int i = 0; i < gvData.Rows.Count; i++)
            {
                CheckBox ck = gvData.Rows[i].Cells[3].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    if (gvData.Rows[i].Cells[1].Text.ToString() == "超级管理员")
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('该角色是基础角色，不能删除')", true);
                        return;
                    }
                    int um = Convert.ToInt32(info.GetScalar("select count(*) from userinfo where roleid = '" + gvData.Rows[i].Cells[0].Text + "'"));

                    if (um > 0)
                    {
                        ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('所选角色有所属用户，不能删除')", true);
                        return;
                    }
                    info.Open("delete from role where roleid ='" + gvData.Rows[i].Cells[0].Text + "'");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);

                    info.Close();

                    Bind();
                }
                else
                {
                    n++;
                }
                if (n == this.gvData.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }

        }
    }
}
